package com.yanqu.road.dao.impl.activity.liondance;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.liondance.LionDanceUserApplyData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LionDanceUserApplyDataDaoImpl extends TempDataObjectDao<LionDanceUserApplyData> {

    public int getMaxApplyId(int activityId, long userId) {
        String sql = "select ifnull(max(`applyNo`),0) as maxId from t_u_activity_lion_dance_apply  where `activityId`=? and `userId`=? ";
        ResultSet rs = null;
        CallableStatement call = null;
        int maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


    public static String getLionDanceUserApplyDataSql = "select * from `t_u_activity_lion_dance_apply` where `activityId`=? order by applyTimes desc";
    public Map<Long, List<LionDanceUserApplyData>> getLionDanceUserApplyData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<LionDanceUserApplyData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getLionDanceUserApplyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                LionDanceUserApplyData data = parse(rs);
                if(!result.containsKey(data.getUserId())){
                    result.put(data.getUserId(), new ArrayList<>());
                }
                if(result.get(data.getUserId()).size() < 20){
                    result.get(data.getUserId()).add(data);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }


    public static String updateLionDanceUserApplyDataSql = "update `t_u_activity_lion_dance_apply` set applyGoodsId=?,applyTimes=?,status=?,serverId=?,friendId=?,friend=?,fromType=? where activityId=? and userId=? and applyNo=?";
    @Override
    protected boolean updateData(LionDanceUserApplyData lionDanceUserApplyData) {
        lionDanceUserApplyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getApplyGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getApplyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getFriendId()));
        para.put(i++, new DbParameter(Types.VARCHAR, lionDanceUserApplyData.getFriend() == null ? "" : JSON.toJSONString(lionDanceUserApplyData.getFriend())));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getFromType()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getApplyNo()));
        if (getDb().executeNoneQuery(updateLionDanceUserApplyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLionDanceUserApplyDataSql = "insert into `t_u_activity_lion_dance_apply`(activityId,userId,applyGoodsId,applyTimes,status,serverId,friendId,friend,fromType,applyNo) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(LionDanceUserApplyData lionDanceUserApplyData) {
        lionDanceUserApplyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getApplyGoodsId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getApplyTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getStatus()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, lionDanceUserApplyData.getFriendId()));
        para.put(i++, new DbParameter(Types.VARCHAR, lionDanceUserApplyData.getFriend() == null ? "" : JSON.toJSONString(lionDanceUserApplyData.getFriend())));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getFromType()));
        para.put(i++, new DbParameter(Types.INTEGER, lionDanceUserApplyData.getApplyNo()));
        if (getDb().executeNoneQuery(insertLionDanceUserApplyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected LionDanceUserApplyData parseData(ResultSet rs) throws SQLException {
        LionDanceUserApplyData data = new LionDanceUserApplyData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setApplyGoodsId(rs.getInt("applyGoodsId"));
        data.setApplyTimes(rs.getLong("applyTimes"));
        data.setStatus(rs.getInt("status"));
        data.setServerId(rs.getLong("serverId"));
        data.setFriendId(rs.getLong("friendId"));
        String str = rs.getString("friend");
        if(!StringUtils.isNullOrEmpty(str)) {
            data.setFriend(JSON.parseObject(str, UserBaseInfo.class));
        }
        data.setApplyNo(rs.getInt("applyNo"));
        data.setFromType(rs.getInt("fromType"));
        return data;
    }
}
